// pages/subPages/addressManage/addressManage.js
const app = getApp();
const api = require('../../../utils/api');
const {myRequest} = require('../../../utils/myRequest');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    proName:'',
    city:'',
    county:'',
    proId:null,
    cityId:null,
    countyId:null,
    addressList:[],
    provinceList:[],
    cityList:[],
    countyList:[],
    mobile:'',
    name:'',
    detailAddress:'',
    addnewFlag:false,
    change:false,
    item1:null
  },
  start(e){
    console.log(e);
    let start = e.changedTouches[0].clientX;
    this.setData({
      start:start
    })
  },
  end(e){
    console.log(e);
    let index = e.currentTarget.dataset.index;
    let end = e.changedTouches[0].clientX;
    let scrollLeft = this.data.scrollLeft;
    if(end - this.data.start < -10){
        scrollLeft[index] = '200rpx';
    }else{
      scrollLeft[index] = 0;
    }
    this.setData({
      scrollLeft:scrollLeft
    })
  },
  // 修改地址确认按钮
  change_confirm(){
      if(!this.data.proId){
        wx.showToast({
          title: '请选择省份',
          icon: 'none',
          duration: 1500,
          mask: true,
        });
        return;
      }
      if(!this.data.cityId){
        wx.showToast({
          title: '请选择城市',
          icon: 'none',
          duration: 1500,
          mask: true,
        });
        return;
      }
      if(!this.data.countyId){
        wx.showToast({
          title: '请选择区/县',
          icon: 'none',
          duration: 1500,
          mask: true,
        });
        return;
      }
      if(!this.data.detailAddress){
        wx.showToast({
          title: '请输入您的详细地址',
          icon: 'none',
          duration: 1500,
          mask: true,
        });
        return;
      }
      if(!this.data.name){
        wx.showToast({
          title: '请输入您的姓名',
          icon: 'none',
          duration: 1500,
          mask: true,
        });
        return;
      }
      if(!this.data.mobile){
        wx.showToast({
          title: '请输入您的联系电话',
          icon: 'none',
          duration: 1500,
          mask: true,
        });
        return;
      }
      let {id,is_default}=this.data.item1;
      if(is_default){
        is_default=true;
      }
      myRequest({
        url:api.addressSave,
        data:{
          id:id,
          name:this.data.name,
          mobile:this.data.mobile,
          province_id:this.data.proId,
          city_id:this.data.cityId,
          district_id:this.data.countyId,
          address:this.data.detailAddress,
          is_default:is_default
        },
        method:'POST',
        success:(res)=>{
          console.log(res);
          // this.resetbtn();
          this.setData({
            change:false
          })
          this.getAddressList();
        }
      })
  },
  // 设置为默认地址
  moren(){
    // console.log(e);
    let {id,name,mobile,province_id, city_id,district_id,address,is_default} = this.data.item1;
    let item1=this.data.item1;
    is_default=!is_default;
    item1.is_default=is_default;
    myRequest({
      url:api.addressSave,
      data:{
        id:id,
        name:name,
        mobile:mobile,
        province_id:province_id,
        city_id:city_id,
        district_id:district_id,
        address:address,
        is_default:is_default
      },
      method:'POST',
      success:(res)=>{
        console.log(res);
        this.setData({
          item1:item1
        })
        this.getAddressList();
      }
    })
  },
  // 修改地址
  change(e){
    let change=!this.data.change;
    let item=e.currentTarget.dataset.item;
    if(change){
      
      this.setData({
        change:change,
        item1:item,
        detailAddress:item.address,
        name:item.name,
        mobile:item.mobile,
        proName:item.province_name,
        city:item.city_name,
        county:item.district_name,
        proId:item.province_id,
        cityId:item.city_id,
        countyId:item.district_id
      })
    }else{
      this.setData({
        item1:null,
        change:change
      })
    }
  },
  // 删除地址
  delete(e){
    let id = e.currentTarget.dataset.id;
    myRequest({
      url:api.addressDelete,
      data:{
        id:id
      },
      method:'POST',
      success:(res)=>{
        console.log(res);
        this.getAddressList();
      }
    })
  },
  // 获取姓名
  getname(e){
    if(e.detail.value.trim()){
      console.log(1);
      let name=e.detail.value.trim();
      this.setData({
        name:name
      })
    }else{
      console.log(2);
      return;
    }
  },
  // 获取电话
  getmobile(e){
    console.log(e);
    if(e.detail.value.trim()){
      console.log(1);
      let mobile=e.detail.value.trim();
      this.setData({
        mobile:mobile
      })
    }else{
      console.log(2);
      return;
    }
  },
  addnewFlag(){
    let flag = !this.data.addnewFlag;
    this.resetbtn();
    this.setData({
      addnewFlag:flag
    })
  },
  changeproflag(e){
    console.log(e);
    let flag = !this.data.proFlag;
    console.log(flag);
    this.setData({
      proFlag:flag
    })
  },
  // 获取详细地址
  getDetail(e){
    if(e.detail.value.trim()){
      console.log(1);
      let detailAddress=e.detail.value.trim();
      this.setData({
        detailAddress:detailAddress
      })
    }else{
      console.log(2);
      return;
    }
  },
  // 新增地址确认按钮
  confirmbtn(){
    let def;
    if(this.data.addressList.length==0){
       def = true;
    }else{
        def = false;
    }
    if(!this.data.proId){
      wx.showToast({
        title: '请选择省份',
        icon: 'none',
        duration: 1500,
        mask: true,
      });
      return;
    }
    if(!this.data.cityId){
      wx.showToast({
        title: '请选择城市',
        icon: 'none',
        duration: 1500,
        mask: true,
      });
      return;
    }
    if(!this.data.countyId){
      wx.showToast({
        title: '请选择区/县',
        icon: 'none',
        duration: 1500,
        mask: true,
      });
      return;
    }
    if(!this.data.detailAddress){
      wx.showToast({
        title: '请输入您的详细地址',
        icon: 'none',
        duration: 1500,
        mask: true,
      });
      return;
    }
    if(!this.data.name){
      wx.showToast({
        title: '请输入您的姓名',
        icon: 'none',
        duration: 1500,
        mask: true,
      });
      return;
    }
    if(!this.data.mobile){
      wx.showToast({
        title: '请输入您的联系电话',
        icon: 'none',
        duration: 1500,
        mask: true,
      });
      return;
    }


    myRequest({
      url:api.addressSave,
      data:{
        name:this.data.name,
        mobile:this.data.mobile,
        province_id:this.data.proId,
        city_id:this.data.cityId,
        district_id:this.data.countyId,
        address:this.data.detailAddress,
        is_default:def
      },
      method:'POST',
      success:(res)=>{
        console.log(res);
        this.resetbtn();
        this.setData({
          addnewFlag:false
        })
        this.getAddressList();
      }
    })
  },
  // 重置按钮
  resetbtn(){
    this.setData({
      proName:'',
      city:'',
      county:'',
      proId:null,
      cityId:null,
      countyId:null,
      cityList:[],
      countyList:[],
      mobile:'',
      name:'',
      detailAddress:'',
      proFlag:false,
      cityFlag:false,
      countyFlag:false,
    })
  },
  // 获取省份名称
  getProName(e){
    let index = e.detail.value;    
    let proName = this.data.provinceList[index].name;
    let proId=this.data.provinceList[index].id;
    this.setData({
      proName:proName,
      proId:proId,
    })
    myRequest({
      url:api.regionList,
      data:{
        parentId:proId
      },
      method:'GET',
      success:(res)=>{
        console.log(res);
        let cityList = res.data.data;
        this.setData({
          cityList:cityList,
          city:'',
          county:''
        })

      }
    })

  },
  // 获取城市名称
  getCityName(e){
    let index = e.detail.value;
    let city = this.data.cityList[index].name;
    let id = this.data.cityList[index].id;
    this.setData({
      city:city,
      cityId:id,
    })
    myRequest({
      url:api.regionList,
      data:{
        parentId:id
      },
      method:'GET',
      success:(res)=>{
        console.log(res);
        let countyList = res.data.data;
        this.setData({
          countyList:countyList,
          county:''
        })

      }
    })

  },
  // 获取区名称
  getCountyName(e){
    let index = e.detail.value;
    let county = this.data.countyList[index].name;
    let id = this.data.countyList[index].id;
    this.setData({
      county:county,
      countyId:id,
    })
  },
  getAddressList(){
    myRequest({
      url:api.addressList,
      data:{
      },
      method:'GET',
      success:(res)=>{
        console.log(res);
        let addressList=res.data.data;
        let scrollLeft = [];
        addressList.some(function(item,index){
          scrollLeft[index] = 0;
          if(item.is_default){
            // let id = item.id;
            wx.setStorage({
              key: 'addressId',
              data: item.id,
              success: (result)=>{
                console.log('已保存默认地址id')
              },
            });
          }
        })
        this.setData({
          addressList:addressList,
          scrollLeft:scrollLeft
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        myRequest({
          url:api.regionList,
          data:{
            parentId:1
          },
          method:'GET',
          success:(res)=>{
            console.log(res);
            let provinceList = res.data.data;
            this.setData({
              provinceList:provinceList
            })

          }
        });
        this.getAddressList();
        
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})